package com.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.entidades.Administrador;

@Stateless
public class AdministradorDAO extends GenericDAO<Administrador> {

	@PersistenceContext(unitName = "Projeto2POD")
	private EntityManager em;
	
	public AdministradorDAO() {
		super(Administrador.class);
	}

//	public AdministradorDAO(Class<Administrador> entityClass) {
//		super(entityClass);
//	}
	
	public Administrador findByLogin(String login){		
		try {
			Query q = em.createQuery("select a from Administrador a where a.login='"+login+"'");
			Administrador a =(Administrador)q.getSingleResult();
			return a;
		} catch (NoResultException e) {
			return null;
		}
	}

	public Administrador findByCPF(String CPF){
		try {
			Query q = em.createQuery("select a from Administrador a where a.CPF LIKE '%" + CPF + "%'");
			return (Administrador)q.getSingleResult();
		} catch (NoResultException e) {
			return null;
		} catch (NonUniqueResultException e) {
			return null;
		}
	}

	public Administrador findbyNome(String nome) {
		try {
			Query q = em.createQuery("select a from Administrador a where a.nome LIKE '%" + nome + "%'");
			return (Administrador)q.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}

	}

}
